package org.sanguine.tests.tenderpro;

import org.sanguine.tests.tenderpro.actions.Action;
import org.sanguine.tests.tenderpro.actions.ActionFactory;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Check XML file and count an specified element
 * Command line format: DOMCheckXML FileXML AnElement
 *
 */

 public class DOMCheckXML {
    private static XmlWorker worker;

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: DOMCheckXML FileXML AnElement [-schema]");
            System.out.println("Where:");
            System.out.println("\tFileXML  - target XML file");
            System.out.println("\tAnElement - searching element");
            System.out.println("\t-schema (optional) - use XSD schema validation");
            System.exit(1);
        }

        boolean xsd = false;
        if (args.length > 2) {
            List<String> optionalArgs = Arrays.asList(Arrays.copyOfRange(args, 2, args.length));
            if (optionalArgs.contains("-schema")) xsd = true;
        }
        try {
            worker = new XmlWorker(args[0], xsd);
        } catch (XMLStreamException e) {
            System.err.printf("File %s isn't valid or not found\n", args[0]);
            System.exit(2);
        } catch (ParserConfigurationException e) {
            System.err.println("XML Parser Configuration error");
            e.printStackTrace();
            System.exit(2);
        } catch (SAXException e) {
            System.err.println("XML Content is not valid");
            e.printStackTrace();
            System.exit(2);
        } catch (IOException e) {
            System.err.println("Can't read XML file: " + args[0]);
//            e.printStackTrace();
            System.exit(2);
        }

        worker.setElement(args[1]);
        int c = worker.doCountElement();

        if (c < 0) System.out.println("The element " + args[1] + " is not found");
        else System.out.println(args[1] + ": " + c + (c == 1 ? " time" : " times"));

        doActions(Arrays.asList(args));
    }

    private static void doActions(List<String> params) {
        if (!params.contains("-action") || !params.contains("-actionXML")) return;

        String actionName = params.get(params.indexOf("-action") + 1);
        if (actionName == null || actionName.isEmpty()) {
            System.err.println("Action name is not set");
            return;
        }

        String actionXmlFile = params.get(params.indexOf("-actionXML") + 1);
        if (actionXmlFile == null || actionXmlFile.isEmpty()) {
            System.err.println("Action XML file is not set");
            return;
        }

        String actionClass = getActionClass(actionName, actionXmlFile);
        if (actionClass != null) {
            Action action = ActionFactory.getInstance(actionClass);
            if (action != null) action.doAction(params);
        }
    }

    private static String getActionClass(String actionName, String actionXmlFile) {
        //TODO get class from XML
        return "org.sanguine.tests.tenderpro.actions.CountAttribute";
    }
}
